package it.orma.iridehr.service;

import it.orma.iridehr.entities.Provincia;
import it.orma.iridehr.exceptions.DatiException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service("tipologicheService")
public class TipologicheService {

	@Autowired
	private ApplicationContext context;

	public List<Provincia> listaProvince() throws DatiException{
		SAXReader saxReader = new SAXReader();
		List<Provincia> provinceArray = new ArrayList<Provincia>();
		try {
			Document documento = saxReader.read(context.getResource("classpath:province.xml").getInputStream());
			List<Element> province = documento.selectNodes("//provincia");
			for (Iterator<Element> iterator = province.iterator(); iterator.hasNext();) {
				Element node = iterator.next();
				provinceArray.add(new Provincia(node.getTextTrim()));
			}
		} catch (DocumentException e) {
			throw new DatiException(e.getMessage(),e);
		} catch (IOException e) {
			throw new DatiException(e.getMessage(),e);
		}
		Collections.sort(provinceArray);
		return provinceArray;
	}

	public List<String> listaScuole(String filtro, boolean onlyVisible) throws DatiException{
		SAXReader saxReader = new SAXReader();
		List<String> lista = new ArrayList<String>();
		try {
			Document documento = saxReader.read(context.getResource("classpath:studi.xml").getInputStream());
			if (onlyVisible){
				filtro+="[@visibile='true']";
			}
			List<Element> studi = documento.selectNodes(filtro);
			for (Iterator<Element> iterator = studi.iterator(); iterator.hasNext();){
				Element node = iterator.next();
				lista.add(node.getTextTrim());
			}
		} catch (DocumentException e) {
			throw new DatiException(e.getMessage(),e);
		} catch (IOException e) {
			throw new DatiException(e.getMessage(),e);
		}
		Collections.sort(lista);
		return lista;
	}

	public List<String> listaNazionalita() throws DatiException{
		SAXReader saxReader = new SAXReader();
		List<String> nazionalitaArray = new ArrayList<String>();
		try {
			Document documento = saxReader.read(context.getResource("classpath:nazionalita.xml").getInputStream());
			List<Element> nazionalita = documento.selectNodes("//nazionalita");
			for (Iterator<Element> iterator = nazionalita.iterator(); iterator.hasNext();) {
				Element node = iterator.next();
				nazionalitaArray.add(node.getTextTrim());
			}
		} catch (DocumentException e) {
			throw new DatiException(e.getMessage(),e);
		} catch (IOException e) {
			throw new DatiException(e.getMessage(),e);
		}
		//Collections.sort(nazionalitaArray);
		return nazionalitaArray;
	}

	public List<String> listaLingue() throws DatiException{
		SAXReader saxReader = new SAXReader();
		List<String> linguaArray = new ArrayList<String>();
		try {
			Document documento = saxReader.read(context.getResource("classpath:lingue.xml").getInputStream());
			List<Element> lingue = documento.selectNodes("//lingua");
			for (Iterator<Element> iterator = lingue.iterator(); iterator.hasNext();) {
				Element node = iterator.next();
				linguaArray.add(node.getTextTrim());
			}
		} catch (DocumentException e) {
			throw new DatiException(e.getMessage(),e);
		} catch (IOException e) {
			throw new DatiException(e.getMessage(),e);
		}
		//Collections.sort(nazionalitaArray);
		return linguaArray;
	}

	public List<String> listaProgrammi(String tipo) throws DatiException{
		SAXReader saxReader = new SAXReader();
		List<String> array = new ArrayList<String>();
		try {
			Document documento = saxReader.read(context.getResource("classpath:prog-"+tipo+".xml").getInputStream());
			List<Element> lingue = documento.selectNodes("//programma");
			for (Iterator<Element> iterator = lingue.iterator(); iterator.hasNext();) {
				Element node = iterator.next();
				array.add(node.getTextTrim());
			}
		} catch (DocumentException e) {
			throw new DatiException(e.getMessage(),e);
		} catch (IOException e) {
			throw new DatiException(e.getMessage(),e);
		}
		Collections.sort(array);
		return array;
	}

	public List<Pair<Integer, String>> listaLivelliDiStudio(){
		List<Pair<Integer, String>> lista = new ArrayList<Pair<Integer,String>>();
		lista.add(new MutablePair<Integer, String>(1, "Media inferiore"));
		lista.add(new MutablePair<Integer, String>(2, "Diploma"));
		lista.add(new MutablePair<Integer, String>(3, "Corso post diploma"));
		lista.add(new MutablePair<Integer, String>(4, "Laurea"));
		lista.add(new MutablePair<Integer, String>(5, "Master"));
		lista.add(new MutablePair<Integer, String>(6, "Altro"));
		return lista;
	}

	public Pair<Integer, String> getLivelloDiStudio(Integer livello){
		List<Pair<Integer, String>> lista = listaLivelliDiStudio();
		for (Iterator iterator = lista.iterator(); iterator.hasNext();) {
			Pair<Integer, String> pair = (Pair<Integer, String>) iterator
					.next();
			if (pair.getLeft().intValue() == livello.intValue()){
				return pair;
			}

		}
		return null;
	}
}
